const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'
const ctime = require('../../time/conversion')
//获取自己部门的所有员工信息
router.post('/getTrainInfo',async (req, res) => {
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        if(response_temp.length > 0) {
            sql = 'SELECT workNumber,name,age,stationName,trainType,trainSite,trainHour,trainContent,trainScore,trainStart,trainEnd from department_employees WHERE departmentId = ?'
            let data = [response_temp[0].departmentId]
            if(req.body.workNumber!=null){
                sql = 'SELECT workNumber,name,age,stationName,trainType,trainSite,trainHour,trainContent,trainScore,trainStart,trainEnd from department_employees WHERE departmentId = ? and workNumber = ?'
                data = [response_temp[0].departmentId,req.body.workNumber]
                console.log("指定工号的查询")
                console.log(data)
            }
            //查找自己部门的员工信息
            db.base(sql, data, response => {

                for (let i = 0; i < response.length; i++) {
                    ctime.formatUTC(response[i].trainStart, result => {
                        response[i].trainStart = result
                    })
                    ctime.formatUTC(response[i].trainEnd, result => {
                        response[i].trainEnd = result
                    })
                }
                res.json(response)
            })
        }else{
            res.json({
                result:"error",
                reason:"无权限"
            })
        }
    })

})

module.exports = router
